//-----------------------------------------------------------------------
// <copyright file="ShippingStatusReportService.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------	

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using MIMS.WRS.Entities;
using MIMS.WRS.Data;
    
namespace MIMS.WRS.Business
{   
	/// <summary>
	/// 
	/// </summary> 
    public class ShippingStatusReportService
    {
        #region ShippingStatusReport
		public static void CreateShippingStatusReport(ShippingStatusReport shippingStatusReport)
        {            
            try
            {
				using (TransactionScope scope = new TransactionScope()) 
				{
//					if (shippingStatusReport.ShippingLineCode.Length > 0) 
//					{
//						ShippingLineDAO slDAO = new ShippingLineDAO();
//						ShippingLine sl = slDAO.GetShippingLineByCode(shippingStatusReport.ShippingLineCode);
//						if (sl == null) 
//						{
//							sl = new ShippingLine();
//							sl.Abbr = shippingStatusReport.ShippingLineCode;
//							sl.URL = shippingStatusReport.ShippingLineUrl;
//							slDAO.CreateShippingLine(sl);
//						}						
//						shippingStatusReport.ShippingLineID = sl.ID;
//					}

//					if (shippingStatusReport.ConPoint.Length > 0) 
//					{
//						ConPointDAO conPointDAO = new ConPointDAO();
//						ConPoint conPoint = conPointDAO.GetConPoint(shippingStatusReport.ConPoint);
//						if (conPoint == null) 
//						{
//							conPoint = new ConPoint();
//							conPoint.Description = shippingStatusReport.ConPoint;						
//							conPointDAO.CreateConPoint(conPoint);
//						}
//						shippingStatusReport.ConPointID = conPoint.ConPointID;
//					}
//
//					if (shippingStatusReport.PONo_Supplier != Nulls.NullInteger) 
//					{
//						string supplierCode = shippingStatusReport.PONo_Supplier.ToString().PadLeft(3, '0');
//						Supplier sup = SupplierService.GetSupplier(supplierCode);
//						if (sup == null )
//						{
//							sup = new Supplier();
//							sup.SupplierCode = supplierCode;
//							sup.SupplierName = "Supplier " + supplierCode;
//							SupplierService.CreateSupplier(sup);
//						}
//					}
					
					ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
					shippingStatusReportDAO.CreateShippingStatusReport(shippingStatusReport);
					scope.Complete();
				}
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessCreateShippingStatusReportException, ex);
            }
        }        

        public static void UpdateShippingStatusReport(ShippingStatusReport shippingStatusReport)
        {            
            try
            {
				int result = CheckReqConstraint(shippingStatusReport);

				using (TransactionScope scope = new TransactionScope()) 
				{	
//					if (shippingStatusReport.ShippingLineCode.Length > 0) 
//					{
//						ShippingLineDAO slDAO = new ShippingLineDAO();
//						ShippingLine sl = slDAO.GetShippingLineByCode(shippingStatusReport.ShippingLineCode);
//						if (sl == null) 
//						{
//							sl = new ShippingLine();
//							sl.Abbr = shippingStatusReport.ShippingLineCode;
//							sl.URL = shippingStatusReport.ShippingLineUrl;
//							slDAO.CreateShippingLine(sl);
//						}						
//						shippingStatusReport.ShippingLineID = sl.ID;
//					}

//					if (shippingStatusReport.ConPoint.Length > 0) 
//					{
//						ConPointDAO conPointDAO = new ConPointDAO();
//						ConPoint conPoint = conPointDAO.GetConPoint(shippingStatusReport.ConPoint);
//						if (conPoint == null) 
//						{
//							conPoint = new ConPoint();
//							conPoint.Description = shippingStatusReport.ConPoint;						
//							conPointDAO.CreateConPoint(conPoint);
//						}
//						shippingStatusReport.ConPointID = conPoint.ConPointID;
//					}
//
//					if (shippingStatusReport.PONo_Supplier != Nulls.NullInteger) 
//					{
//						string supplierCode = shippingStatusReport.PONo_Supplier.ToString().PadLeft(3, '0');
//						Supplier sup = SupplierService.GetSupplier(supplierCode);
//						if (sup == null )
//						{
//							sup = new Supplier();
//							sup.SupplierCode = supplierCode;
//							sup.SupplierName = "Supplier " + supplierCode;
//							SupplierService.CreateSupplier(sup);
//						}
//					}				
				
					ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
					// if Consolidation, OrderType, ETADate or Shipped Date has been modified 
					// then replicate to other POs have same req
					if (result > 0) 
					{
						ShippingStatusReportCollection ssrCol = shippingStatusReportDAO.GetShippingStatusReports(shippingStatusReport.REQ, shippingStatusReport.OfficeID);
						foreach (ShippingStatusReport ssr in ssrCol) 
						{
							ssr.PONo_Consolidation = shippingStatusReport.PONo_Consolidation;
							ssr.PONo_Type = shippingStatusReport.PONo_Type;
							ssr.ETA = shippingStatusReport.ETA;
							ssr.ShippingDate = shippingStatusReport.ShippingDate;
							ssr.POInOpera = shippingStatusReport.POInOpera;
							ssr.PI = shippingStatusReport.PI;
							ssr.AccNo = shippingStatusReport.AccNo;
							ssr.CustPONo = shippingStatusReport.CustPONo;
							ssr.OnHold = shippingStatusReport.OnHold;
							ssr.ArrivalPort = shippingStatusReport.ArrivalPort;
							ssr.Vessel = shippingStatusReport.Vessel;
							ssr.FeederVessel = shippingStatusReport.FeederVessel;
							ssr.TargetETD = shippingStatusReport.TargetETD;
							ssr.IsNomShip = shippingStatusReport.IsNomShip;
							ssr.EstActBlank = shippingStatusReport.EstActBlank;
							ssr.ContainerNo = shippingStatusReport.ContainerNo;
							ssr.ContainerDeliveried = shippingStatusReport.ContainerDeliveried;
							//ssr.DocReceived  = shippingStatusReport.DocReceived;
							ssr.CompletedDate  = shippingStatusReport.CompletedDate;
							ssr.SInvoiced  = shippingStatusReport.SInvoiced;
							ssr.SInvoicedPd = shippingStatusReport.SInvoicedPd;
							ssr.CustomerOrLocation = shippingStatusReport.CustomerOrLocation;
							ssr.FinalDestination = shippingStatusReport.FinalDestination;
							ssr.ShippingLineID = shippingStatusReport.ShippingLineID;
							ssr.SalesOrder = shippingStatusReport.SalesOrder;
							ssr.ResponsibleID = shippingStatusReport.ResponsibleID;
							shippingStatusReportDAO.UpdateShippingStatusReport(ssr);
						}
					}
					shippingStatusReportDAO.UpdateShippingStatusReport(shippingStatusReport);

					scope.Complete();
				}
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessUpdateShippingStatusReportException, ex);
            }
        }        

        public static void DeleteShippingStatusReport(int id)
        {            
            try
            {
                ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
                shippingStatusReportDAO.DeleteShippingStatusReport(id);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessDeleteShippingStatusReportException, ex);
            }
        }
        
        public static ShippingStatusReport GetShippingStatusReport(int id)
        {            
            try
            {
                ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
                return shippingStatusReportDAO.GetShippingStatusReport(id);                
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetShippingStatusReportException, ex);
            }
        }                


		public static ShippingStatusReport GetShippingStatusReportByReq(int req)
		{            
			try
			{
				ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
				return shippingStatusReportDAO.GetShippingStatusReportByReq(req);                
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessGetShippingStatusReportByReqException", ex);
			}
		} 

        public static ShippingStatusReportCollection GetShippingStatusReports(ShippingStatusReportColumns orderBy, OrderDirection orderDirection)
        {            
            try
            {
                ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
                return shippingStatusReportDAO.GetShippingStatusReports(orderBy, orderDirection);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetShippingStatusReportsException, ex);
            }
        }        

        public static ShippingStatusReportCollection GetShippingStatusReports(ShippingStatusReportColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
                return shippingStatusReportDAO.GetShippingStatusReports(orderBy, orderDirection, page, pageSize, out totalRecords);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetShippingStatusReportsException, ex);
            }
        }        

        #endregion

		public static ShippingStatusReportCollection GetPaymentNotPaidList() 
		{
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetPaymentNotPaidList();
		}

		public static ShippingStatusReportCollection GetETAHasPastList()  
		{
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetETAHasPastList();
		}

		public  static ShippingStatusReportCollection GetDocNotReceivedList()  
		{
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetDocNotReceivedList();
		}

		public static IDataReader GetMissingDataReport(int officeID, int type) 
		{       
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetMissingDataReport(officeID, type);
		}

		public static IDataReader GetLateFactoryETDReport(int officeID, int type) 
		{       
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetLateFactoryETDReport(officeID, type);
		}

		public static IDataReader GetLateShippedDateReport(int officeID, int type) 
		{       
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetLateShippedDateReport(officeID, type);
		}

		public static IDataReader GetSupplierIncompleteReport(int officeID, int supp) 
		{       
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetSupplierIncompleteReport(officeID, supp);
		}

		public static int GetTotalSalesOrderFound(int officeID, int poType, int salesOrder) 
		{       
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetTotalSalesOrderFound(officeID, poType, salesOrder);
		}

		public static IDataReader GetSupplierList(int officeID) 
		{       
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetSupplierList(officeID);
		}

		public static IDataReader GetShippingStatusReports(int officeID, int sortType, int poType, int salesOrder)
		{            
			int totalRecords = 0;				
			return GetShippingStatusReports(officeID, sortType, poType, salesOrder, 0, 0, out totalRecords);
		}    

		public static IDataReader GetShippingStatusReports(int officeID, int sortType, int poType, int salesOrder, int page, int pageSize, out int totalRecords)
		{            
			try
			{
				ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
				return shippingStatusReportDAO.GetShippingStatusReports(officeID, sortType, poType, salesOrder, page, pageSize, out totalRecords);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetShippingStatusReportsException, ex);
			}
		}        

		public static IDataReader GetShippingStatusReports(int officeID, int supplierCode, int orderNumber, DateTime orderDate, bool incompleted) 
		{            
			try
			{
				ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
				return shippingStatusReportDAO.GetShippingStatusReports(officeID, supplierCode, orderNumber, orderDate, incompleted);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetShippingStatusReportsException, ex);
			}
		}    

		public static IDataReader GetContainerDeliveredReport(int officeID, DateTime fromDate, DateTime toDate, int type) 
		{       
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetContainerDeliveredReport(officeID, fromDate, toDate, type);
		}

		public static IDataReader GetNonPaymentReport(int officeID, int type) 
		{       
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetNonPaymentReport(officeID, type);
		}

		public static IDataReader GetNonPaymentLess7Report(int officeID, int type) 
		{       
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetNonPaymentLess7Report(officeID, type);
		}

		public static IDataReader GetOrderNotPlacedWithFactoryReport(int officeID, int type, int conpointID, string supplierCode) 
		{       
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetOrderNotPlacedWithFactoryReport(officeID, type, conpointID, supplierCode);
		}

		public static int GetNextReqNumber(int officeID) 
		{
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetNextReqNumber(officeID);
		}
    
		public static int GetNextSalesOrderNumber() 
		{
			ShippingStatusReportDAO ssrDAO = new ShippingStatusReportDAO();
			return ssrDAO.GetNextSalesOrderNumber();
		}

		public static int CheckUniqueSalesOrder(int salesOrder, int req) 
		{
			try
			{
				ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
				return shippingStatusReportDAO.CheckUniqueSalesOrder(salesOrder, req);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetShippingStatusReportsException, ex);
			}
		}

		public static int CheckReqConstraint(ShippingStatusReport shippingStatusReport) 
		{
			try
			{
				ShippingStatusReportDAO shippingStatusReportDAO = new ShippingStatusReportDAO();
				ShippingStatusReportCollection ssrCol = shippingStatusReportDAO.GetShippingStatusReports(shippingStatusReport.REQ, shippingStatusReport.OfficeID);
				
				int totalErrorCode = 0;
				if (ssrCol.Count > 0) 
				{
					ShippingStatusReport ssr = ssrCol[0];
					if (shippingStatusReport.PONo_Consolidation != ssr.PONo_Consolidation)
						totalErrorCode += (int)ECheckPOReqErrorCode.ConsolidationNotMatch;
					if (shippingStatusReport.PONo_Type != ssr.PONo_Type)
						totalErrorCode += (int)ECheckPOReqErrorCode.OrderTypeNotMatch;
					if (shippingStatusReport.ETA != ssr.ETA)
						totalErrorCode += (int)ECheckPOReqErrorCode.ETADateNotMatch;
					if (shippingStatusReport.ShippingDate != ssr.ShippingDate)
						totalErrorCode += (int)ECheckPOReqErrorCode.ShippedDateNotMatch;
					//TargetETD
					if (shippingStatusReport.TargetETD != ssr.TargetETD)
						totalErrorCode += (int)ECheckPOReqErrorCode.TargetETDNotMatch;
					
					//IsNomShip
					if (shippingStatusReport.IsNomShip != ssr.IsNomShip)
						totalErrorCode += (int)ECheckPOReqErrorCode.NomShipNotMatch;
					//EstActBlank
					if (shippingStatusReport.EstActBlank != ssr.EstActBlank  && !(ssr.EstActBlank == ""))
						totalErrorCode += (int)ECheckPOReqErrorCode.EstActNotMatch;

					if (shippingStatusReport.POInOpera != ssr.POInOpera)
						totalErrorCode += (int)ECheckPOReqErrorCode.POInOperaNotMatch;
					if (shippingStatusReport.PI.Replace(" ", "").ToLower() != ssr.PI.Replace(" ", "").ToLower())
						totalErrorCode += (int)ECheckPOReqErrorCode.PISCNotMatch;
					if (shippingStatusReport.AccNo.Replace(" ", "").ToLower() != ssr.AccNo.Replace(" ", "").ToLower())
						totalErrorCode += (int)ECheckPOReqErrorCode.AccNoNotMatch;
					if (shippingStatusReport.CustPONo.Replace(" ", "").ToLower() != ssr.CustPONo.Replace(" ", "").ToLower())
						totalErrorCode += (int)ECheckPOReqErrorCode.CustPONoNotMatch;
					if (shippingStatusReport.OnHold != ssr.OnHold)
						totalErrorCode += (int)ECheckPOReqErrorCode.OnHoldNotMatch;
					if (shippingStatusReport.ArrivalPort.Replace(" ", "").ToLower() != ssr.ArrivalPort.Replace(" ", "").ToLower())
						totalErrorCode += (int)ECheckPOReqErrorCode.ArrivalPortNotMatch;
					if (shippingStatusReport.Vessel.Replace(" ", "").ToLower() != ssr.Vessel.Replace(" ", "").ToLower())
						totalErrorCode += (int)ECheckPOReqErrorCode.VesselNotMatch;
					//FeederVesse
					if (shippingStatusReport.FeederVessel.Replace(" ", "").ToLower() != ssr.FeederVessel.Replace(" ", "").ToLower())
						totalErrorCode += (int)ECheckPOReqErrorCode.FeederVesselNotMatch;

					if (shippingStatusReport.ContainerNo != ssr.ContainerNo)
						totalErrorCode += (int)ECheckPOReqErrorCode.ContainerNoNotMatch;
					if (shippingStatusReport.ContainerDeliveried != ssr.ContainerDeliveried)
						totalErrorCode += (int)ECheckPOReqErrorCode.ContDelNotMatch;
					//DocReceived
					//if (shippingStatusReport.DocReceived != ssr.DocReceived)
					//	totalErrorCode += (int)ECheckPOReqErrorCode.DocsRecvNotMatch;
					if (shippingStatusReport.CompletedDate != ssr.CompletedDate)
						totalErrorCode += (int)ECheckPOReqErrorCode.DocsRecvNotMatch;
					if (shippingStatusReport.SInvoiced != ssr.SInvoiced)
						totalErrorCode += (int)ECheckPOReqErrorCode.SInvoiceNoNotMatch;
					if (shippingStatusReport.SInvoicedPd != ssr.SInvoicedPd)
						totalErrorCode += (int)ECheckPOReqErrorCode.SInvoicePDNotMatch;
					if (shippingStatusReport.CustomerOrLocation != ssr.CustomerOrLocation)
						totalErrorCode += (int)ECheckPOReqErrorCode.CustomerNotMatch;
					if (shippingStatusReport.FinalDestination != ssr.FinalDestination)
						totalErrorCode += (int)ECheckPOReqErrorCode.FinalDestinationNotMatch;
					if (shippingStatusReport.ShippingLineID != ssr.ShippingLineID)
						totalErrorCode += (int)ECheckPOReqErrorCode.ShippingLineNotMatch;
					if (shippingStatusReport.SalesOrder != ssr.SalesOrder)
						totalErrorCode += (int)ECheckPOReqErrorCode.SalesOrderNotMatch;
					if (shippingStatusReport.ResponsibleID != ssr.ResponsibleID)
						totalErrorCode += (int)ECheckPOReqErrorCode.ResponsibleNotMatch;
				}
				return totalErrorCode;								
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetShippingStatusReportsException, ex);
			}
		}		
    }

	public enum ECheckPOReqErrorCode 
	{
		NoError = 0,
		ConsolidationNotMatch = 1,
		OrderTypeNotMatch = 2,
		ETADateNotMatch = 4,
		ShippedDateNotMatch = 8,
		ConPointNotMatch = 16,
		POInOperaNotMatch = 32,
		PISCNotMatch = 64,
		AccNoNotMatch = 128,
		CustPONoNotMatch = 256,
		OnHoldNotMatch = 512,
		ArrivalPortNotMatch = 1024,
		VesselNotMatch = 2048,
		ContainerNoNotMatch = 4096,
		ContDelNotMatch = 8192,
		DocsRecvNotMatch = 16384,
		CompletedNotMatch = 32768,
		SInvoiceNoNotMatch = 65536,
		SInvoicePDNotMatch = 131072,
		CustomerNotMatch = 262144,
		FinalDestinationNotMatch = 524288,
		ShippingLineNotMatch = 1048576,
		SalesOrderNotMatch = 2097152,
		ResponsibleNotMatch = 4194304,
		FeederVesselNotMatch = 8388608,
		NomShipNotMatch = 16777216,
		EstActNotMatch = 33554432,
		TargetETDNotMatch = 67108864,
	}

        
}